<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

session_start();

require_once("database/profile_handler.php");
require_once 'database/message_handler.php';

if (!isset($_SESSION['Username']) || !isset($_SESSION['Password'])) {
    header('Location: index.php');
}



$sub = str_replace("+", " ", $_GET['sub']);
$sub2 = $_GET['sub'];
$msgid = $_GET['msgid'];
$m = new message_handler();
$pr = new profile_handler();

//$idsub = $msgid. "&subject=". $_GET['sub'];
//                     header('Location:message_thread.php?msgid='. $idsub);

$m->retr_message_col($_SESSION["Username"]);


$parse = $m->parse_group_members($m->result[3]);
for ($j = 0; $j < count($parse); $j++) {
    if ($m->result[4][$j] == $_GET['msgid']) {
        for ($i = 0; $i < count($parse[$j])-1; $i++) {
            $members[$i] = $parse[$j][$i] . " ";
        }
        $g_id = $m->result[2][$j];
        //subject
        $subj = $m->result[0][$j];
        //$s = $m->result[0][$j];
        //message body
        
        $body = $m->result[1][$j];

        //    }
    }
}
$s = str_replace(" ", "+", $subj[0]);
$idsub = $msgid . "&subject=" . $s;

if (isset($_POST['replyMessage'])) {

                            $body = $_POST['bodyReply'];
                            $m->reply_message($g_id, $msgid, $_SESSION['Username'], $body);
                            header('Location: message_thread.php?msgid='. $idsub);
                            //header("Location: homepage.php?replymsg=1&msgid=". $msgid. "&sub=". $sub);
                            //header("Location: javascript:viewMessageThread(". $msgid. ",". "\"". $_GET['sub']. "\"". " )");
                        }

//
//if ($_GET['suc'] == 1) {
//    header('Location: message_thread.php?msgid='. $idsub);
//    echo "aa";
//}

?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

        <script type="text/javascript" src="script/validator.js"></script>
        <script type="text/javascript" src="script/calendar.js"></script>
        <script type="text/javascript" src="script/ajax.js"></script>
        <script type="text/javascript">
            document.getElementById("replies").innerHTML = getMessages(5,<?php echo $g_id;?>,<?php echo $msgid;?>,99);
        </script>
        <link rel="stylesheet" href="style/style.css" />
    </head>
    <body onload="loadPageHide()">

         <div id="container">
        <div id="headerBody">
            <form id="Search" name="Search" method ="GET" action="search_result.php">

                <input type="text" id="SearchBar" name="SearchBar"/>

                <input type="submit" id="SearchOk" name="SearchOk" value="Search"/>
            </form>
            <?php if ($_SESSION['Username'] != null) {
            ?>
                <a id="headerLink" href="javascript:viewHome();" id="headerLink">Home</a>
                <a id="headerLink" href="javascript:showPopupProfile();" id="headerLink">Profile</a>
                <a id="headerLink" href="javascript:viewMessage();" id="headerLink">Message</a>
                <a id="headerLink" href="javascript:viewUploader();" id="headerLink">Uploader</a>
                <a id="headerLink" href="javascript:viewGallery();" id="headerLink">Gallery</a>
                <a href="database/login.php?logout=1" id="logout_link" >Logout</a>
                <a href="chat.php" id="logout_link" >Chat</a>
                <a href="javascript:viewProfile();" id="logout_link" ><?php echo $_SESSION['Username'];?></a>
                <a href="javascript:showNotif('<?php echo $_SESSION['Username']; ?>');" id="headerLink">Notification</a>
                <!-- tampilin jumlah notif -->
            <?php
                require_once 'database/notif_handler.php';

                $notif = new notif_handler();
                $jml = 0;

                $notif->retr_notification($_SESSION['Username']);
                if (count($notif->berita) > 0 && $notif->berita[0] != null) {
                    for ($i = 0; $i < count($notif->berita); $i++) {
                        if ($notif->read[$i] == 0) {
                            $jml++;
                        }
                    }
                    if ($jml > 0)
                        echo "<font color='red'>(" . $jml . ")</font>";
                }
                else {
                    echo "";
                }
            ?>
                <div id="profile_popup">
                </div>
                <div id="notif">
                    <div id="notif2">
                    </div>
                </div>
                <a href="profile.php" id="logout_link" ><?php echo $u; ?></a>
            <?php } else { ?>
                <a href="index.php" id="logout_link" >Login</a>
            <?php
            }
            ?>
        </div>
        <div id=""><img src="images/flickertiew_logo.gif" alt=""/></div>
        <!-- CONTENT dimulai disini -->
        <div id="centerBody">
        <div id ="content">
            <html>
                <head>
                    <title>Reply to Thread</title>
                </head>
                <body>

            <div id="title_reply"><strong>Thread : </strong></div>
                    <br/><br/>
                    <div id="new_reply_thread">

                        <?php

                        echo "Subject : " . $subj. "<br/>";

                         echo "<font color='green'>Between : </font>";
                                for ($i = 0; $i < count($members); $i++) {
                                    $pr->get_nama_lengkap($members[$i]);
                                    if ($members[$i] == $_SESSION['Username']){
                                        echo "<a href='otherprofile.php?UserTujuan=". $members[$i]. "' id='link_name'>". "You" . "</a>, ";
                                    }
                                    else{
                                        echo "<a href='otherprofile.php?UserTujuan=". $members[$i]. "' id='link_name'>". $pr->result[0] . "</a>, ";
                                    }
                                }


                        echo "<br/>";

                        echo "<br/><div id='message_body'>";
                        echo $body . "<br/><br/>";
                        echo "</div><br/><br/>";


                        ?>
                    </div>
                    <p><div id="title_post_reply"><strong>Post Reply : </strong></div></p>
                    <div id="post_reply">
                        <br/>
                    <form action="<?php echo "message_thread.php?msgid=" . $idsub; ?>" method="POST">
                        <textarea name="bodyReply" cols="50" rows="4"></textarea><br/>
                        <input type="submit" name="replyMessage" id="button_reply" value="Reply" />
                    </form>
                    </div>
                    <br/><br/>

                   <!-- Si AJAX ditaro disini !-->
                    <div id="select_pagination">
                        View
                        <select id="view_pagination" name="view_pagination" onchange="getMessages(this.value,<?php echo $g_id;?>,<?php echo $msgid;?>,99)">
                            <option value="5" selected>5</option>
                            <option value="10">10</option>
                            <option value="15">15</option>
                            <option value="20">20</option>
                        </select>
                        per page
                    </div>

                   <div id="replies"></div>
                   </body>
            </html>
        </div>
        </div>
        <div id="footerBody" class="group_content">
                <div id="footer" align="center">
                    Copyright &copy; 2011<br/>
                    Created by <a href="http://www.facebook.com/Gunawan.Robert" id="headerLink">Robert Gunawan</a>
                                                    , <a href="http://www.facebook.com/danz3m" id="headerLink">Danang Tri Massandy</a>,
                    <a href="http://www.facebook.com/achmad.giovani" id="headerLink">Achmad Giovani</a>
                </div>
            </div>
        </div>
                   

    </body>
</html>
